import 'answer.dart';

/// 讨论话题数据结构
class DiscussionTopic {
  final String topicId;
  final String topicTitle;
  final String topicDescription;
  final String creatorId;
  final String creatorDisplayName;
  final String creatorProfileImage;
  final DateTime creationTime;
  final DateTime lastUpdateTime;
  final int viewerCount;
  final int supportCount;
  final List<String> categoryTags;
  final List<ResponseItem> responseList;
  final bool isTopicResolved;

  DiscussionTopic({
    required this.topicId,
    required this.topicTitle,
    required this.topicDescription,
    required this.creatorId,
    required this.creatorDisplayName,
    required this.creatorProfileImage,
    required this.creationTime,
    required this.lastUpdateTime,
    this.viewerCount = 0,
    this.supportCount = 0,
    this.categoryTags = const [],
    this.responseList = const [],
    this.isTopicResolved = false,
  });

  /// 从数据映射构建对象
  factory DiscussionTopic.fromDataMap(Map<String, dynamic> dataMap) {
    return DiscussionTopic(
      topicId: dataMap['topicId'] as String,
      topicTitle: dataMap['topicTitle'] as String,
      topicDescription: dataMap['topicDescription'] as String,
      creatorId: dataMap['creatorId'] as String,
      creatorDisplayName: dataMap['creatorDisplayName'] as String,
      creatorProfileImage: dataMap['creatorProfileImage'] as String,
      creationTime: DateTime.parse(dataMap['creationTime'] as String),
      lastUpdateTime: DateTime.parse(dataMap['lastUpdateTime'] as String),
      viewerCount: dataMap['viewerCount'] as int? ?? 0,
      supportCount: dataMap['supportCount'] as int? ?? 0,
      categoryTags: List<String>.from(dataMap['categoryTags'] as List? ?? []),
      responseList:
          (dataMap['responseList'] as List?)
              ?.map((responseData) => ResponseItem.fromDataMap(responseData))
              .toList() ??
          [],
      isTopicResolved: dataMap['isTopicResolved'] as bool? ?? false,
    );
  }

  /// 转换为数据映射
  Map<String, dynamic> toDataMap() {
    return {
      'topicId': topicId,
      'topicTitle': topicTitle,
      'topicDescription': topicDescription,
      'creatorId': creatorId,
      'creatorDisplayName': creatorDisplayName,
      'creatorProfileImage': creatorProfileImage,
      'creationTime': creationTime.toIso8601String(),
      'lastUpdateTime': lastUpdateTime.toIso8601String(),
      'viewerCount': viewerCount,
      'supportCount': supportCount,
      'categoryTags': categoryTags,
      'responseList':
          responseList.map((response) => response.toDataMap()).toList(),
      'isTopicResolved': isTopicResolved,
    };
  }

  /// 创建修改版本
  DiscussionTopic createModifiedVersion({
    String? topicId,
    String? topicTitle,
    String? topicDescription,
    String? creatorId,
    String? creatorDisplayName,
    String? creatorProfileImage,
    DateTime? creationTime,
    DateTime? lastUpdateTime,
    int? viewerCount,
    int? supportCount,
    List<String>? categoryTags,
    List<ResponseItem>? responseList,
    bool? isTopicResolved,
  }) {
    return DiscussionTopic(
      topicId: topicId ?? this.topicId,
      topicTitle: topicTitle ?? this.topicTitle,
      topicDescription: topicDescription ?? this.topicDescription,
      creatorId: creatorId ?? this.creatorId,
      creatorDisplayName: creatorDisplayName ?? this.creatorDisplayName,
      creatorProfileImage: creatorProfileImage ?? this.creatorProfileImage,
      creationTime: creationTime ?? this.creationTime,
      lastUpdateTime: lastUpdateTime ?? this.lastUpdateTime,
      viewerCount: viewerCount ?? this.viewerCount,
      supportCount: supportCount ?? this.supportCount,
      categoryTags: categoryTags ?? this.categoryTags,
      responseList: responseList ?? this.responseList,
      isTopicResolved: isTopicResolved ?? this.isTopicResolved,
    );
  }

  /// 获取回复数量
  int get responseCount => responseList.length;

  /// 获取最新回复时间
  DateTime? get latestResponseTime {
    if (responseList.isEmpty) return null;
    return responseList
        .map((response) => response.publishedTime)
        .reduce((a, b) => a.isAfter(b) ? a : b);
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is DiscussionTopic && other.topicId == topicId;
  }

  @override
  int get hashCode => topicId.hashCode;

  @override
  String toString() {
    return 'DiscussionTopic(id: $topicId, title: $topicTitle, creator: $creatorDisplayName, responses: $responseCount)';
  }
}
